package org.example.struct.array;

import java.util.*;

/**
 * @author liyishan
 * @date 2024/8/7 10:13
 * @apiNote
 */

public class Test00599FindRestaurant {
    public String[] findRestaurant(String[] list1, String[] list2) {
        Map<String,Integer> map = new HashMap<>();
        for(int i =0;i<list1.length;i++){
            map.put(list1[i],i);
        }
        List<String> ret = new ArrayList<>();
        int indexSum = Integer.MAX_VALUE;
        for(int i =0;i<list2.length;i++){
            if(map.containsKey(list2[i])){
                int j = map.get(list2[i]);
                if(i+j<indexSum){
                    ret.clear();
                    ret.add(list2[i]);
                    indexSum = i+j;
                }else if(i+j == indexSum){
                    ret.add(list2[i]);
                }
            }
        }
        return ret.toArray(new String[0]);
    }
}
